package com.app.controller;

import com.app.common.Result;
import com.app.mapper.UserMapper;
import com.app.model.IdeaModel;
import com.app.model.UserModel;
import com.app.service.IdeaService;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 意见建议
 * -意见建议
 * ->点击进入：判断是否登录 获取手机号（可修改），标题，意见内容，是否匿名  /idea/save
 * <p>
 * 意见建议：
 * 提交 路径 /idea/save 请求方式 post 参数 title content openid
 * 查询 路径 /idea/get  请求方式 post 参数 userId
 */
@Controller
@RestController
@RequestMapping("/api/idea")
@ResponseBody
public class IdeaController {
    @Autowired
    private IdeaService ideaService;

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    //保存建议
    @PostMapping("/save")
    public Result<Object> save(@RequestBody IdeaModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        try {
            String id = TokenUtils.nothingness(token);
            if (id == null) {
                return Result.error(405, "token不存在");
            }
            UserModel user = userMapper.getByOpenid(id);
            if (user == null) {
                return Result.error(405, "用户不存在");
            }
            String title = model.getTitle();
            String content = model.getContent();
            IdeaModel idea = new IdeaModel();
            idea.setTitle(title);
            idea.setContent(content);
            idea.setUserId(user.getId());
            idea.setMobile(user.getMobile());
            idea.setCreateTime(LocalDateTime.now());
            ideaService.add(idea);
            return Result.success(idea);
        } catch (Exception e) {
            return Result.error(405, "登录失效，请重新登录");
        }
    }

    //通过id查询建议
    @PostMapping("/get")
    public Result<Object> getById(@RequestBody IdeaModel model,
                                  HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        try {
            String id = TokenUtils.nothingness(token);
            if (id == null) {
                return Result.error(405, "token不存在");
            }
            UserModel user = userMapper.getByOpenid(id);
            if (user == null) {
                return Result.error(405, "用户不存在");
            }
            Integer page = model.getPage();
            Integer limit = model.getLimit();
            List<IdeaModel> idea = ideaService.selectContent(page, limit, user.getId());
            String faceUrl = userService.getFaceUrl(user.getId());
            for (int i = 0; i < idea.size(); i++) {
                idea.get(i).setFaceUrl(faceUrl);
            }
            return Result.success(idea);
        } catch (Exception e) {
            return Result.error(405, "登录失效，请重新登录");
        }
    }


    //意见建议管理
    //userId，头像，昵称，意见标题，手机号，提交时间  page页码 limit每页显示的条数
    @GetMapping("/selectIdea")
    public Result<Object> selectIdea(Integer page, Integer limit, String keyword) {
        return Result.success(ideaService.selectIdea(page, limit, keyword));
    }

    //查看详情
    @GetMapping("/selectIdeaContent")
    public Result<Object> selectIdeaContent(Integer id) {
        return Result.success(ideaService.selectIdeaContent(id));
    }
}
